package com.company;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 夏令时的问题
 * https://www.cnblogs.com/wbo112/p/15776907.html
 *
 * @date : 2023-07-28 上午11:01
 */

public class P063_RandomDate {

    private static final String DATE_PATTERN = "yyyyMMdd";
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:hh:ss";


    public static void main(String[] args) throws ParseException {
        // for (int i = 0; i < 100000; i++) {
        //     // String date = getRandomDate();
        //     String date = "19860505";
        //     if (!getPreviousDate(date).equals(getPreviousDay(date))) {
        //         System.out.println(date);
        //         System.out.println(getPreviousDate(date));
        //         System.out.println(getPreviousDay(date));
        //         break;
        //     }
        // }1986-05-05 00:12:00
        // 1986-05-03 23:11:00


        SimpleDateFormat newFormat = new SimpleDateFormat(DATE_TIME_PATTERN);
        System.out.println(newFormat.parse("1986-05-05 12:34:00"));
        // System.out.println(newFormat.parse("1986-05-05 00:12:00"));

    }


    private static String getPreviousDate(String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
        SimpleDateFormat newFormat = new SimpleDateFormat(DATE_TIME_PATTERN);
        long time = format.parse(date).getTime();
        System.out.println(new Date(time));
        System.out.println(newFormat.format(new Date(time)));
        System.out.println(newFormat.format(new Date(time - 24 * 60 * 60 * 1000)));
        return format.format(new Date(time - 24 * 60 * 60 * 1000));
    }


    private static String getPreviousDay(String date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
        LocalDate localDate = LocalDate.parse(date, formatter);
        return localDate.minusDays(1).format(formatter);
    }

    private static String getRandomDate() {
        long randomMillisSinceEpoch = ThreadLocalRandom
                .current()
                .nextLong(0, System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
        return format.format(new Date(randomMillisSinceEpoch));
    }
}
